package com.xposed.aliproxy.utils;

import org.apache.http.protocol.HTTP;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;

public class Utils {

    public static String formatStr2Decimal(String str) {
        return new DecimalFormat("0.00").format(Double.parseDouble(str) / 100.0d);
    }

    public static String formatDouble2Str(double d) {
        return new DecimalFormat("0").format(d);
    }

    public static String formatStr2Dec(String str) {
        return Utils.formatDouble2Str(Double.parseDouble(new DecimalFormat("0.00").format(Double.parseDouble(str))) * 100.0d);
    }

    public static String str2Builder(String str) {
        for (int length = str.length(); length < 4; length++) {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("0");
            stringBuilder.append(str);
            str = stringBuilder.toString();
        }
        return str;
    }

    public static boolean isNull(String str) {
        return str == null || str.length() == 0;
    }

    /**
     * 登录签名
     */
    public static String signLogin(Map<String, String> map, String str) {
        String str2 = "";
        Object[] toArray = map.keySet().toArray();
        Arrays.sort(toArray);
        for (Object obj : toArray) {
            Object obj2 = map.get(obj);
            if (!(obj2 == null || obj2.equals("") || obj.equals("sign"))) {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append(str2);
                stringBuilder.append(obj);
                stringBuilder.append("=");
                stringBuilder.append(obj2.toString());
                stringBuilder.append("&");
                str2 = stringBuilder.toString();
            }
        }
        StringBuilder stringBuilder2 = new StringBuilder();
        stringBuilder2.append(str2);
        stringBuilder2.append(str);
        String stringBuilder3 = stringBuilder2.toString();
        PrintStream printStream = System.err;
        StringBuilder stringBuilder4 = new StringBuilder();
        stringBuilder4.append("macStr:");
        stringBuilder4.append(stringBuilder3);
        printStream.println(stringBuilder4.toString());
        stringBuilder3 = Utils.md5Str(stringBuilder3).toLowerCase();
        printStream = System.err;
        stringBuilder4 = new StringBuilder();
        stringBuilder4.append("mac:");
        stringBuilder4.append(stringBuilder3);
        printStream.println(stringBuilder4.toString());
        return stringBuilder3;
    }

    public static String formatJson(JSONObject jSONObject, String str) {
        Map e = Utils.str2Map(jSONObject.toString());
        String str2 = "";
        Object[] toArray = e.keySet().toArray();
        Arrays.sort(toArray);
        for (Object obj : toArray) {
            Object obj2 = e.get(obj);
            if (!(obj2 == null || obj2.equals("") || obj.equals("signature"))) {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append(str2);
                stringBuilder.append(obj);
                stringBuilder.append("=");
                stringBuilder.append(obj2.toString());
                stringBuilder.append("&");
                str2 = stringBuilder.toString();
            }
        }
        StringBuilder stringBuilder2 = new StringBuilder();
        stringBuilder2.append(str2);
        stringBuilder2.append(str);
        String stringBuilder3 = stringBuilder2.toString();
        PrintStream printStream = System.err;
        StringBuilder stringBuilder4 = new StringBuilder();
        stringBuilder4.append("macStr:");
        stringBuilder4.append(stringBuilder3);
        printStream.println(stringBuilder4.toString());
        stringBuilder3 = Utils.md5Str(stringBuilder3);
        printStream = System.err;
        stringBuilder4 = new StringBuilder();
        stringBuilder4.append("mac:");
        stringBuilder4.append(stringBuilder3);
        printStream.println(stringBuilder4.toString());
        return stringBuilder3;
    }

    public static HashMap<String, String> str2Map(String str) {
        Map hashMap = new HashMap();
        try {
            JSONObject jSONObject = new JSONObject(str);
            Iterator keys = jSONObject.keys();
            while (keys.hasNext()) {
                String str2 = (String) keys.next();
                hashMap.put(str2, jSONObject.get(str2).toString());
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return (HashMap) hashMap;
    }

    public static String md5Str(String str) {
        MessageDigest instance = null;
        try {
            instance = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        instance.reset();
        try {
            instance.update(str.getBytes(HTTP.UTF_8));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        byte[] digest = instance.digest();
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < digest.length; i++) {
            if (Integer.toHexString(digest[i] & 255).length() == 1) {
                stringBuffer.append("0");
                stringBuffer.append(Integer.toHexString(digest[i] & 255));
            } else {
                stringBuffer.append(Integer.toHexString(digest[i] & 255));
            }
        }
        return stringBuffer.toString();
    }

    public static String randomStr(int i) {
        String str = "0123456789ABCEDF";
        StringBuilder stringBuilder = new StringBuilder();
        for (int i2 = 0; i2 < i; i2++) {
            int nextInt = new Random().nextInt(str.length());
            stringBuilder.append(str.substring(nextInt, nextInt + 1));
        }
        return stringBuilder.toString();
    }
}
